package net.cyl.listener;


import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import net.cyl.emums.BizCodeEnum;
import net.cyl.emums.EventMessageType;
import net.cyl.exception.BizException;
import net.cyl.model.EventMessage;
import net.cyl.service.ShortLinkService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
//@RabbitListener(queues ="short.link.add.mapping.queue")
@RabbitListener(queuesToDeclare = {@Queue("short_link.delete.mapping.queue")})
@Slf4j
public class ShortLinkDelMappingMQListener {
    @Autowired
    private ShortLinkService shortLinkService;
    @RabbitHandler
    public void shortLinkHandle(EventMessage eventMessage, Message message, Channel channel) throws IOException {
        log.info("接收到消息: {}", eventMessage);

        long tag = message.getMessageProperties().getDeliveryTag();

        try  {
            eventMessage.setEventType(EventMessageType.SHORT_LINK_DELETE_MAPPING.name());
            shortLinkService.handleDelShortLink(eventMessage);
        }
        catch (Exception e) {
            log.error("短链映射消息处理失败", e);
            throw new BizException(BizCodeEnum.MQ_CONSUMER_EXCEPTION);
        }

        log.info("短链映射消息处理成功");

    }
}
